package me.raih.service.impl;

import me.raih.mapper.CategoryMapper;
import me.raih.mapper.WaresMapper;
import me.raih.pojo.po.Wares;
import me.raih.pojo.vo.PageBean;
import me.raih.service.WaresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author Raih
 * @Time 2020/11/28  15:30
 */
@Service
@Transactional
public class WareServiceImpl implements WaresService {

    @Autowired
    WaresMapper waresMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    PageBean<Wares> pageBean;

    @Override
    public List<Wares> findWares(String waresCategory) {

        List<Wares> waresList = waresMapper.findAllWares();
        //
        //if("管理员".equals(wareCategory)){
        //
        //}
        if(Objects.isNull(waresList)){
            return new ArrayList<>();
        }
        return waresList;
    }

    @Override
    public PageBean<Wares> findWaresByPage(int currentPage, int pageSize) {

        if(currentPage < 0){
            currentPage = 0;
        }

        List<Wares> waresList = waresMapper.findWaresByPage((currentPage)*5, pageSize);
        //获取总数据数量
        //int maxNumber = waresMapper.maxNumber();
        int maxNumber = 100;

        pageBean.setList(waresList);        //设置泛型存放集合
        int size ;
        pageBean.setPageSize(pageSize);     //设置多少个为一页
        if(maxNumber%pageSize == 0){
            size = maxNumber/pageSize;
        }else {
            size = (maxNumber/pageSize)+1;
        }
        pageBean.setCurrentPage(currentPage);   //设置当前页面

        pageBean.setTotalCount(maxNumber);   //设置总共的数据  千万不能弄混了

        pageBean.setTotalPage(size);    //设置共多少页

        return pageBean;

    }

    @Override
    public PageBean<Wares> findWaresByPageAndName(int currentPage, int pageSize, String name, String no,String modifyMan,String status) {

        //if(name != null){
        //    name = "%"+name+"%";
        //}
        if("".equals(name) || name == null){
            name = null;
        }else {
            name = "%"+name+"%";
        }
        if("".equals(no) || no == null){
            no = null;
        }
        else {
            no = "%"+no+"%";
        }
        if("".equals(modifyMan) || modifyMan == null){
            modifyMan = null;
        }
        else {
            modifyMan = "%"+modifyMan+"%";
        }
        if("".equals(status)){
            status = null;
        }
        //
        //if(currentPage%5 ==0 ){
        //    currentPage = currentPage/5;
        //}else {
        //    currentPage = (currentPage/5)+1;
        //}

        List<Wares> waresList = waresMapper.findWaresByPageAndName((currentPage)*pageSize,
                pageSize,
                name,
                no,
                modifyMan,
                status);
        //设置当前页
        pageBean.setCurrentPage(currentPage);
        //设置查出来的信息
        pageBean.setList(waresList);

        int maxNumber = waresMapper.maxNumber(name,no,modifyMan,status);
        //int maxNumber = waresList.size();
        //设置符合的最大数据
        pageBean.setTotalCount(maxNumber);
        int size;
        if(pageSize == 0){
            pageSize = 10;
        }
        //设置每页多少个数据
        pageBean.setPageSize(pageSize);
        if(maxNumber%pageSize == 0){
            size = maxNumber/pageSize;
        }else {
            size = (maxNumber/pageSize)+1;
        }
        pageBean.setTotalPage(size);    //设置共多少页

        return pageBean;
    }

    @Override
    public boolean canAddWares(Wares wares) {
        //先从数据库中查一下该 no 是否已经存在
        Wares waresByNo = waresMapper.findWaresByNo(wares.getNo());
        //如果查不出来，就说明没有，可以添加
        if(waresByNo == null){
            //查一下该商品对应的分类
            int category_no = categoryMapper.findCategoryByName(wares.getCategoryName());
            //补充商品详细信息
            wares.setCategoryNo(category_no);
            wares.setCreateTime(new Date());
            wares.setUpdateTime(new Date());
            //添加该 商品 到数据库中
            int i = waresMapper.addWaresByWares(wares);
            if(i > 0){
                return true;
            }
        }
        //如果说，存在
        return false;
    }

    @Override
    public Wares findWaresByNo(String no) {

        Wares wares = waresMapper.findWaresByNo(no);

        return wares;
    }

    @Override
    public boolean canChangeStatus(String no, String status) {

        if(no == null || status == null){
            return false;
        }
        int i = waresMapper.updateStatusByNo(no, status);

        if(i > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateWares(Wares wares) {

        int i = waresMapper.updateWares(wares);
        if(i > 0){
            return true;
        }
        return false;
    }

    @Override
    public PageBean<Wares> findWaresByCategoryNo(int categoryNo, int currentPage, int pageSize) {
        //规定页面大小就为 10
        pageSize = 10;

        if(currentPage < 0){
            currentPage = 1;
        }
        List<Wares> waresList = waresMapper.findWaresByCategoryNo(categoryNo, (currentPage - 1) * pageSize, pageSize);

        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setList(waresList);
        int maxNum = waresMapper.findMaxNumberByCategoryNo(categoryNo);
        pageBean.setTotalCount(maxNum);
        pageBean.setTotalPage(maxNum%pageSize==0?(maxNum/pageSize):(maxNum/pageSize)+1);

        return pageBean;
    }

    @Override
    public PageBean<Wares> findWaresByWaresName(String name, int currentPage, int pageSize) {
        pageSize = 20;
        if(currentPage < 0){
            currentPage = 1;
        }
        //变一变咯
        name = '%'+name+"%";

        List<Wares> waresList = waresMapper.findWaresByWaresName(name, (currentPage - 1) * pageSize, pageSize);

        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setList(waresList);
        int maxNum = waresMapper.findMaxNumberByWaresName(name);
        pageBean.setTotalCount(maxNum);
        pageBean.setTotalPage(maxNum%pageSize == 0?(maxNum/pageSize):(maxNum/pageSize)+1);

        return pageBean;
    }

    @Override
    public PageBean<Wares> searchWares(int currentPage, String waresName) {




        return null;
    }

    @Override
    public boolean deleteWares(String no) {
        //先查到商品信息
        Wares waresByNo = waresMapper.findWaresByNo(no);
        //设置一下修改时间
        waresByNo.setUpdateTime(new Date());
        //将状态改成下架
        waresByNo.setWaresStatus("下架");
        //添加到 下架商品表中
        int i = waresMapper.addWaresByWaresForDelete(waresByNo);
        if(i == 1){
            //若添加到下架表中，通过商品编号 下架商品
            int j = waresMapper.deleteWares(no);
            return  j == 1;
        }
        return false;
    }

    @Override
    public int findWaresNumber() {
        int i = waresMapper.findWaresNumber();
        return Math.max(i, 0);
    }

    @Override
    public int findTodayWaresNumber() {
        int i = waresMapper.findTodayWaresNumber();
        return Math.max(i, 0);
    }

    @Override
    public int findYesterdayWaresNumber() {
        int i = waresMapper.findYesterdayWaresNumber();
        return Math.max(i, 0);
    }

    @Override
    public int findCurrWeekWaresNumber() {
        int i = waresMapper.findCurrWeekWaresNumber();
        return Math.max(i, 0);
    }

    @Override
    public int findCurrMonthWaresNumber() {

        int i = waresMapper.findCurrMonthWaresNumber();
        return Math.max(i, 0);
    }

    @Override
    public PageBean<Wares> findDelWaresByPageAndName(int currentPage, int totalPage, int pageSize, String param) {

        if("search".equals(param)){
            currentPage = 0;
        }else if("forward".equals(param)){
            if(currentPage > 0){
                currentPage --;
            }
        }else if("begin".equals(param)){
            if(currentPage != 0){
                currentPage = 0;
            }
        }else if("nextPage".equals(param)){
            if(currentPage != totalPage){
                currentPage ++;
            }
        }else if("end".equals(param)){
            if(currentPage != totalPage){
                currentPage = totalPage;
            }
        }else{
            currentPage = 0;
        }

        List<Wares> waresList = waresMapper.findDelWaresByPageAndName(currentPage, pageSize);
        int maxNumber = waresMapper.maxDelNumber(currentPage, pageSize);

        pageBean.setTotalCount(maxNumber);
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalPage(maxNumber%pageSize==0?(maxNumber/pageSize):((maxNumber/pageSize)+1));
        pageBean.setList(waresList);

        return pageBean;
    }

    @Override
    public Wares findDelWaresByNo(String no) {
        Wares wares = waresMapper.findDelWaresByNo(no);
        return wares;
    }

    @Override
    public boolean addDelWares(Wares wares) {
        //设置更新时间
        wares.setUpdateTime(new Date());
        //添加到商品库
        int j = waresMapper.addWaresByWares(wares);
        if(j == 1){
            //从下架商品库中移除
            int i = waresMapper.deleteDelWares(wares.getNo());
            return i == 1;
        }
        return false;
    }
}
